import fs from "node:fs"
import { pluginRoot } from "./models/Path.js"

const ver = JSON.parse(fs.readFileSync(`${pluginRoot}/package.json`, "utf-8")).version

logger.info(logger.yellow(`- 正在加载 小月插件(${ver})`))

const files = (fs.readdirSync("plugins/xiaoyue-plugin/apps"))
  .filter(file => file.endsWith(".js"))

const ret = await Promise.allSettled(
  files.map(i => import(`./apps/${i}`))
)

export const apps = {}
for (const i in files) {
  const name = files[i].replace(".js", "")
  if (ret[i].status != "fulfilled") {
    logger.error(`载入插件错误：${logger.red(name)}`)
    logger.error(ret[i].reason)
    continue
  }
  apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]
}

logger.info(logger.green(`- 小月插件(${ver}) 加载完成`))